package com.zhongyu.controller;
import com.zhongyu.pojo.EmailConfig;
import com.zhongyu.pojo.View;
import com.zhongyu.service.EmailService;
import com.zhongyu.service.ViewService;
import com.zhongyu.vo.ResultVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 库存预警
 */
@RestController
@RequestMapping("/kucun/kcyj")
public class ViewController {
    @Resource
    private ViewService viewService;
    @Resource
    private EmailService emailService;

    @GetMapping("/list")
    public ResultVo list() {
        List<View> list = viewService.search();
        return ResultVo.success(list);
    }

    /**
     * 发送警报邮件
     */
    @GetMapping("/send")
    public ResultVo sendAlertMail(String email) {
        try {
            List<View> list = viewService.search();
            EmailConfig emailConfig=new EmailConfig();
            emailConfig.setTo(email);
            emailConfig.setSubject("库存预警信息");
            emailConfig.setText("有商品库存不足，请及时查看补货");
            if (list.size()>0) {
                emailService.sendSimpleMessage(emailConfig.getTo(),emailConfig.getSubject(),emailConfig.getText());
                return ResultVo.success("发送库存预警信息成功");
            } else {
                return ResultVo.success("不存在库存预警");
            }
        } catch (Exception e) {
            return ResultVo.failure("发送失败");
        }

    }
}
